package com.baibei.sdk;

import android.content.Context;
import android.support.test.InstrumentationRegistry;

import com.baibei.ebec.sdk.ApiFactory;
import com.baibei.ebec.sdk.ApiProvider;
import com.baibei.ebec.sdk.TokenInfo;
import com.github.raee.runit.AndroidRUnit4ClassRunner;
import com.rae.swift.session.SessionManager;

import org.junit.Before;
import org.junit.runner.RunWith;

import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;

/**
 * 接口测试
 * Created by ChenRui on 2017/8/3 0003 10:55.
 */
@RunWith(AndroidRUnit4ClassRunner.class)
public class SdkTest {
    protected Context mContext;

    @Before
    public void setup() {
        mContext = InstrumentationRegistry.getContext();
        ApiFactory.init(mContext);

        // 模拟登陆
        TokenInfo token = new TokenInfo("MJX31PEXB0YIYZLKUFXMKZR24I8IW0BT", "");

        SessionManager.getDefault().setUserToken(token);
    }

    protected ApiProvider getApiProvider() {
        return ApiFactory.getInstance();
    }

    protected <T> void runTest(String method, Observable<T> observable) {
        observable.subscribeOn(Schedulers.io()).subscribe(new TestDefaultObserver<T>(method));
    }

    protected <T> void runTest(Observable<T> observable) {
        String methodName = "runTest";
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        for (StackTraceElement element : stackTrace) {
            String method = element.getMethodName();
            if (method.startsWith("test")) {
                methodName = method;
                break;
            }

        }
        runTest(methodName, observable);
    }
}
